﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using Login.Tools;
using MySql.Data.MySqlClient;

namespace Login.view
{
    public partial class UserMain : UserControl
    {
        delegate void HandleInterfaceUpdateDelegate(string text);  //委托，此为重点
        HandleInterfaceUpdateDelegate interfaceUpdateHandle;
        private String id = "";
        private int money = 0;
        JDBCTool jtool;
        GroupBox b1;
        public UserMain()
        {
            InitializeComponent();
        }
        public UserMain(GroupBox temp)
        {
            InitializeComponent();
            b1 = temp;
        }
        

        private void UserMain_Load(object sender, EventArgs e)
        {
            interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(UpdateTextBox);  //实例化委托对象
            jtool = new JDBCTool();
            string[] str = SerialPort.GetPortNames();
            if (str == null)
            {
                MessageBox.Show("本机没有串口！", "Error");
                return;
            }
            else
            {
                comboBox1.Items.Add("no port");
                foreach (String temp in str)
                {
                    comboBox1.Items.Add(temp);
                }
            }
            //设置serialPort
            serialPort1.PortName = "COM1";
            serialPort1.BaudRate = 4800;
            serialPort1.DataBits = 8;
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Console.WriteLine("{0}",comboBox1.Text);
            
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //hilabel.Text = serialPort1.ReadExisting();
            byte[] readBuffer = new byte[serialPort1.ReadBufferSize];
            serialPort1.Read(readBuffer, 0, readBuffer.Length);
            //this.Invoke(interfaceUpdateHandle, new string[] { Encoding.Unicode.GetString(readBuffer) });
            String text="";
            for(int i=0;i<5;i++){
               
                    if (i == 0)
                    {
                        if (readBuffer[0] == 0xff)
                        {
                            text += "id:";
                        }
                        else if (readBuffer[0] == 0xfd)
                        {
                            text += "money:";
                        }
                    }
                    else
                    {
                        if (readBuffer[0] == 0xff)
                        {
                            text += Convert.ToString(readBuffer[i], 16);
                        id += Convert.ToString(readBuffer[i], 16);
                        }
                        else if (readBuffer[0] == 0xfd)
                        {
                            if (i == 1) {
                                money = readBuffer[i] * (int)System.Math.Pow(256, i - 1);
                                text += money;
                            } 
                        }
                        
                    }
                   
                    
                    
               
            }
            if (id != "" && money != -1)
            {
                jtool.getmysqlcon();
                String sql_str = "select * from info WHERE cardid= '" + id +"'";
                MySqlDataReader myreader = jtool.getmysqlread(sql_str);
                if (myreader.Read())
                {
                    String sql = "update info set money= " + money + " where cardid = '" + id + "'";
                    jtool.getmysqlcom(sql);
                    Console.WriteLine(sql);

                }
                
                
            }
            Console.WriteLine(text);
            this.Invoke(interfaceUpdateHandle, text);
        }
        private void UpdateTextBox(string text)
        {
            
            hilabel.Text = text;
        }

        private void sendButton_Click(object sender, EventArgs e)
        {//扣18元
            
            Byte[] byt1=new Byte[1];
            byt1[0]=0xf1;
            serialPort1.Write(byt1,0,1);
            //Console.WriteLine("hi");
        }

        private void addButton_Click_1(object sender, EventArgs e)
        {
            
            Byte[] byt1 = new Byte[1];
            byt1[0] = 0xf2;
            serialPort1.Write(byt1, 0, 1);
            //Console.WriteLine("hi");
        }

        private void sesql_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            b1.Controls.Clear();
            b1.Controls.Add(new UserSelect(b1));
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                byte[] byt = new Byte[2];
                byt[0] = 0xf3;
                byt[1] = Convert.ToByte(textBox1.Text);
                Console.WriteLine(byt[1]);
                serialPort1.Write(byt, 0, 2);
            }
        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
           
            if (comboBox1.Text == "no port")
            {
                MessageBox.Show("未选择串口！");

            }
            else if (comboBox1.Text != null && comboBox1.Text != "")
            {
                serialPort1.PortName = comboBox1.Text;
                serialPort1.Open();
                if (serialPort1.IsOpen == true)
                {
                    controler.Visible = true;
                    MessageBox.Show("串口打开成功！", comboBox1.Text);
                }


            }
        }

        private void hilabel_Click(object sender, EventArgs e)
        {

        }

        private void serialPort1_PinChanged(object sender, SerialPinChangedEventArgs e)
        {

        }

        private void serialPort1_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {

        }

        
    }
}
